const { Fruit } = require('../models/fruit');

const Add = async ctx => {
  ctx.render('add.html',{
    title: '新增水果'
  });
}

const Create = async ctx => {
  let { thumb, name, price } = ctx.request.body;
  let err = 0;
  let desc = '';

  try {
    await Fruit.create({ thumb, name, price, createAt: Date.now()});
    desc = '新增水果成功！';
  } catch (error) {
    desc = '执行出错！';
    err = 10001;
  }

  ctx.render('result.html',{
    err,
    desc,
    page: 1,
    keyword: ''
  });

}

module.exports = {
  'GET /add': Add,
  'POST /create': Create
}